package com.eascs.ticket.ticket.controller;

import java.util.List;

import javax.inject.Inject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.common.util.ObjectUtil;
import com.eascs.common.util.StringUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.ticket.common.entity.EntityUtil;
import com.eascs.ticket.common.entity.StatusConstant;
import com.eascs.ticket.ticket.entity.TicketComment;
import com.eascs.ticket.ticket.entity.TicketCommentDelete;
import com.eascs.ticket.ticket.entity.TicketCommentUpdate;
import com.eascs.ticket.ticket.entity.TicketCommentView;
import com.eascs.ticket.ticket.form.GetEditViewForm;
import com.eascs.ticket.ticket.form.TicketCommentDeleteForm;
import com.eascs.ticket.ticket.form.TicketCommentForm;
import com.eascs.ticket.ticket.form.TicketIdForm;
import com.eascs.ticket.ticket.service.TicketCommentService;
import com.eascs.web.sso.NoPermissionException;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@RestController
public class TicketCommentRestController extends BaseRestController {

	@Inject
	private TicketCommentService ticketCommentService;

	@RequestMapping(value = "/ticket/opt/getCommentEditView")
	public HtmlRestResult getCommentEditView(GetEditViewForm getEditViewForm) {
		TicketComment ticketComment = null;
		if (StringUtil.isEmpty(getEditViewForm.getId())) {
			ticketComment = new TicketComment();
			ticketComment.setTicketId(getEditViewForm.getTicketId());
		} else {
			ticketComment = ticketCommentService.getTicketCommentById(getEditViewForm.getId());
		}
		ViewContext.put("ticketComment", ticketComment);
		return HtmlRestResult.popHtmlFragment("/ticket/opt/getCommentEditView");
	}

	@RequestMapping(value = "/ticket/opt/saveTicketComment")
	public BaseRestResult<String> saveTicketComment(TicketCommentForm ticketCommentForm) {
		if (StringUtil.hasText(ticketCommentForm.getId())) {
			checkPermission(ticketCommentForm.getId());
			TicketCommentUpdate ticketCommentUpdate = new TicketCommentUpdate();
			ObjectUtil.copySimpleProperties(ticketCommentForm, ticketCommentUpdate);
			EntityUtil.initUpdateEntity(ticketCommentUpdate);
			this.ticketCommentService.updateTicketComment(ticketCommentUpdate);
		} else {
			TicketComment ticketComment = new TicketComment();
			ObjectUtil.copySimpleProperties(ticketCommentForm, ticketComment);
			EntityUtil.initInsertEntity(ticketComment);
			ticketComment.setStatus(StatusConstant.STATUS_OK);
			this.ticketCommentService.insertTicketComment(ticketComment);
		}
		return BaseRestResult.refresh();
	}

	@RequestMapping(value = "/ticket/opt/deleteTicketComment")
	public BaseRestResult<String> deleteTicketComment(TicketCommentDeleteForm ticketCommentDeleteForm) {
		checkPermission(ticketCommentDeleteForm.getId());
		TicketCommentDelete ticketCommentDelete = new TicketCommentDelete();
		ticketCommentDelete.setId(ticketCommentDeleteForm.getId());
		EntityUtil.initUpdateEntity(ticketCommentDelete);
		this.ticketCommentService.deleteTicketComment(ticketCommentDelete);
		return BaseRestResult.refresh();
	}

	@RequestMapping(value = "/ticket/opt/getAllTicketCommentView")
	public HtmlRestResult getAllTicketCommentView(TicketIdForm ticketIdForm) {
		List<TicketCommentView> ticketCommentViewList = ticketCommentService.getTicketCommentViewListByTicketId(ticketIdForm.getTicketId());
		ViewContext.put("ticketCommentViewList", ticketCommentViewList);
		return new HtmlRestResult("ticket/opt/getAllTicketCommentView");
	}

	public void checkPermission(String id) {
		User user = UserHolder.getUser();
		if (user.isSuperAdministrator())
			return;
		TicketComment ticketComment = this.ticketCommentService.getTicketCommentById(id);
		if (user.getCode().equals(ticketComment.getCreator()))
			return;
		throw new NoPermissionException("您无权删除！");
	}
}
